﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UFIDA.U9.Base.Doc;
using UFIDA.U9.CBO.SCM.PropertyTypes;
using UFIDA.U9.Cust.YYD.PlugBE.Dtos;
using UFIDA.U9.Cust.YYD.PlugBE.Public;
using UFIDA.U9.MO.MO;
using UFIDA.U9.PM.ASN;
using UFIDA.U9.PM.PO;
using UFSoft.UBF.Business;

namespace UFIDA.U9.Cust.YYD.PlugBE.ASNP
{
    public class AfterUpdating : UFSoft.UBF.Eventing.IEventSubscriber
    {
        public void Notify(params object[] args)
        {
            #region 从事件参数中取得当前业务实体													 
            if (args == null || args.Length == 0 || !(args[0] is UFSoft.UBF.Business.EntityEvent))
                return;
            BusinessEntity.EntityKey key = ((UFSoft.UBF.Business.EntityEvent)args[0]).EntityKey;
            if (key == null)
                return;
            var holder = key.GetEntity() as UFIDA.U9.PM.ASN.ASN;
            if (holder == null)
                return;
            #endregion
            //审核
            if (holder.OriginalData.Status == ASNStatusEnum.Approving && holder.Status == ASNStatusEnum.Approved)
            {
                HttpHelper httpHelper = new HttpHelper();
                JsonConfig config = new JsonConfig();
                httpHelper.URL = config.SelectToken("WMS.URL") + "/openapi/v3/receipt/create-warehouse-receipt-asn";


                WarehouseReceiptCreateOpenV3Req dto = new WarehouseReceiptCreateOpenV3Req();

                dto.tenantId = "1";
                dto.sourceSysType = "ERP";



                dto.billSourceCode = "ERP_ASN";
                dto.billSourceText = "ERP_ASN";
                dto.billSourceNumber = holder.DocNo;
                dto.urgencyDegree = "NORMAL";
                dto.sourceType = "SOURCE_SUPPLIER";
                dto.sourceCode = holder.Supplier.Code;
                dto.plannedTime = holder.BusinessDate.ToString();
                dto.completionTime = holder.BusinessDate.ToString();
                dto.remark = holder.Memo;

                List<WarehouseRequirementCreateOpenV3Req> requirementList = new List<WarehouseRequirementCreateOpenV3Req>();
                foreach (var line in holder.ASNLine)
                {
                    POLine poline = POLine.Finder.FindByID(line.SrcDocInfo.SrcDocLine.EntityID);

                    if (poline != null)
                    {
                        

                        WarehouseRequirementCreateOpenV3Req dtoit2 = new WarehouseRequirementCreateOpenV3Req();
                        dtoit2.materialCode = poline.ItemInfo.ItemCode;
                        dtoit2.supplierCode = poline.Payer.Code;
                        dtoit2.batchNo = poline.POShiplines[0].ItemLotCode;
                        dtoit2.warehouseCode = poline.POShiplines[0].Wh?.Code ?? poline.ItemInfo.ItemID.InventoryInfo.Warehouse.Code;
                        dtoit2.productionDate = poline.PurchaseOrder.BusinessDate.ToString();
                        dtoit2.lineNumber = poline.DocLineNo.ToString();
                        dtoit2.plannedQuantity = poline.PurQtyTU;
                        dtoit2.cargoOwnerType = "FACTORY";
                        dtoit2.cargoOwner = poline.DescFlexSegments.PubDescSeg2;
                        dtoit2.sourceDetailId = line.ID;
                        dtoit2.sourceBillCode = holder.DocNo;
                        dtoit2.sourceDetailLineNumber = line.DocLineNo.ToString();
                        dtoit2.outsource = poline.PurchaseOrder.DocumentType.BusinessType == BusinessTypeEnum.PM055;
                        dtoit2.internalSupplier = poline.PurchaseOrder.DocumentType.Code == "PO06";

                        requirementList.Add(dtoit2);

                    }

                }

                //dto.detailList = detailList;
                dto.requirementList = requirementList;

                httpHelper.Content = JsonConvert.SerializeObject(dto);
                var result = JObject.Parse(httpHelper.HttpPosts("POST"));
                if (result["code"].ToString() != "000000")
                {
                    throw new Exception(result["msg"].ToString());
                }
            }

            //弃审
            if (holder.OriginalData.Status == ASNStatusEnum.Approved && holder.Status == ASNStatusEnum.Opened) 
            {
                HttpHelper httpHelper = new HttpHelper();
                JsonConfig config = new JsonConfig();
                httpHelper.URL = config.SelectToken("WMS.URL") + "/openapi/v3/receipt/delete-warehouse-receipt";


                WMSDeleteDTO dto = new WMSDeleteDTO();
                dto.billSourceNumber = holder.DocNo;

                httpHelper.Content = JsonConvert.SerializeObject(dto);
                var result = JObject.Parse(httpHelper.HttpPosts("POST"));
                if (result["code"].ToString() != "000000")
                {
                    throw new Exception(result["msg"].ToString());
                }
            }
        }
     

    }
}
